package net.ojbk.jiaofu.service;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;

import net.ojbk.dao.UserDataDao;
import net.ojbk.database.DatabaseConnection;
import net.ojbk.jiaofu.dao.SyTaskDao;
import net.ojbk.jiaofu.dao.SyTaskSubmitDao;
import net.ojbk.jiaofu.dao.getUserDataDao;
import net.ojbk.jiaofu.po.SyTask;
import net.ojbk.jiaofu.po.SyTaskSubmit;
import net.ojbk.po.UserData;
import net.ojbk.util.ExcelTool;
import net.ojbk.util.StringTool;

public class StudentService {
	public static String[] outstandingPerson(String taskId) {  //yes
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTaskSubmitDao stsd=new SyTaskSubmitDao(dbcon);
		SyTaskSubmit sts[];
		sts=stsd.queryAllRecordByStatus(taskId);
		if(sts.length>0) {
			String outstandingList[]=new String[sts.length];
			for(int i=0;i<sts.length;i++) {
				outstandingList[i]=sts[i].getUserId();
			}
			return outstandingList;
			}
		return new String[0];
	}
	public static int updateTaskSubmit(String taskId,String userId) {  //yes
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTaskSubmitDao stsd=new SyTaskSubmitDao(dbcon);
		SyTaskSubmit sts=new SyTaskSubmit();
		sts.setTaskId(taskId);
		sts.setUserId(userId);
		sts.setStatus(1);
		if(stsd.updateOneRecord(sts)) {
			dbcon.close();
			return 1;
		}
		dbcon.close();
		return 0;
	}
	public static String[] findUser(String college,String grade,String clas) { //yes
		DatabaseConnection dbconn=new DatabaseConnection();
		getUserDataDao gud=new getUserDataDao(dbconn);
//		SyTaskSubmitDao stsd=new SyTaskSubmitDao(dbconn);
//		SyTaskSubmit sts;
		UserData uds[];
		uds = gud.getUserData(college, grade, clas);
		String userId[] = new String[uds.length];
		if(uds.length>0) {
			for(int i = 0;i<uds.length;i++) {
//				sts=new SyTaskSubmit();
//				sts.setTaskId(taskId);
//				System.out.println(taskId);
//				System.out.println(uds[i].getId());
//				sts.setUserId(uds[i].getId());
//				stsd.addOneRecord(sts);
				userId[i]=uds[i].getId();
			}
			dbconn.close();
			return userId;
		}
		dbconn.close();
		return new String[0];
	}
	public static int messageToExcel(String userId,String taskId,String[] extraInfo) {
		String basicInfo[]=getBasicInfo(userId,taskId); // 获取基础信息
		String[] info = (String[]) ArrayUtils.addAll(basicInfo, extraInfo);
		
		Object[][] data = new Object[1][info.length];
		data[0]=info;
		try {
			ExcelTool.addOneData(data, TaskService.getExcelPath(userId, taskId));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}
	public static String[] getBasicInfoName(String taskId) {  //no
		// TODO 获取 该赛事 就基本的信息的属性名。通过数据库读取，然后分割成字符串数组
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTaskDao std=new SyTaskDao(dbcon);
		SyTask st=new SyTask();
		st.setTaskId(taskId);
		if(std.queryOneRecord(st)) {
			String basciInfo=st.getBasicInfo();
			//System.out.println(basciInfo);
			dbcon.close();
			String strings[]=StringTool.toManyString(basciInfo);
			//System.out.println(strings[1]);
			return strings;
		}
		dbcon.close();
		return new String[0];
	}
	public static String[] getExtraInfoName(String taskId) {    //no
		// TODO 获取  该赛事 额外需要的信息的属性名。通过数据库读取，然后分割成字符串数组
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTask st=new SyTask();
		SyTaskDao sts=new SyTaskDao(dbcon);
		st.setTaskId(taskId);
		if(sts.queryOneRecord(st)){
			String extraInfo=st.getExtraInfo();
			dbcon.close();
			return StringTool.toManyString(extraInfo);
		}
		dbcon.close();
		return new String[0];
	}
	public static void main(String args[]) {
		StudentService ss=new StudentService();
//		int a=ss.findUser("1", null, null,"afefj");
//		System.out.println(a);
//		ss.updateTaskSubmit("ABCDE", "00001");
		String studentId[]=ss.findUser("1", "1", "1");
		System.out.println(studentId.length);
		System.out.println(studentId[0]);
		System.out.println(studentId[1]);
	}
	public static  String[] getBasicInfo(String userId, String taskId) {   //yes
		// TODO 获取 参加赛事动作 的 基本信息。从UserData数据库中获取。
		String[] basciInfoName=getBasicInfoName(taskId);
		String[] basciInfo = new String[basciInfoName.length];
		UserData ud=new UserData();
		ud.setId(userId);
		UserDataDao udd=new UserDataDao();
		if (!udd.queryOneRecord(ud)) {
			return new String[0];
		}
		for (int i=0; i<basciInfoName.length; i++) {   //根据基本信息要求获取信息
			if ("id".equals(basciInfoName[i]))
				basciInfo[i] = ud.getId();
			else if ("email".equals(basciInfoName[i]))
				basciInfo[i] = ud.getEmail();
			else if ("tel".equals(basciInfoName[i]))
				basciInfo[i] = ud.getTel();
			else if ("idcard".equals(basciInfoName[i]))
				basciInfo[i] = ud.getIdcard();
			else if ("grade".equals(basciInfoName[i]))
				basciInfo[i] = ud.getGrade();
			else if ("college".equals(basciInfoName[i]))
				basciInfo[i] = ud.getCollege();
			else if ("clas".equals(basciInfoName[i]))
				basciInfo[i] = ud.getClas();
			else if ("registrationTime".equals(basciInfoName[i]))
				basciInfo[i] = ud.getRegistrationTime();
			else if ("lastIp".equals(basciInfoName[i]))
				basciInfo[i] = ud.getLastIP();
		}
		return basciInfo;
	}
}
